package L612;

import util.TreeNode;

import java.util.*;

/**
 * @program: javase
 * @description: 二叉树，请你返回其按 层序遍历 得到的节点值
 * @author: luolidong
 * @create: 2021-06-12 09:33
 * @version: 1.0
 */
public class Demo {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) return list;
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> clist = new ArrayList<>();
            int len = queue.size();
            for (int i = 0; i < len; i++) {
                TreeNode node = queue.poll();
                clist.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            list.add(clist);
        }
        return list;
    }

    //    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//        Queue<TreeNode> queue = new LinkedList<>();
//        List<List<Integer>> list = new ArrayList<>();
//        if (root == null) return list;
//        queue.offer(root);
//        int count = 0;
//        while (!queue.isEmpty()) {
//            List<Integer> clist = new ArrayList<>();
//            int len = queue.size();
//            for (int i = 0; i < len; i++) {
//                TreeNode node = queue.poll();
//                clist.add(node.val);
//                if (node.left != null) {
//                    queue.offer(node.left);
//                }
//                if (node.right != null) {
//                    queue.offer(node.right);
//                }
//            }
//            count++;
//            if (count % 2 == 0) {
//                Collections.reverse(clist);
//                list.add(clist);
//            } else {
//                list.add(clist);
//            }
//        }
//        return list;
//    }
//    使用双端队列，头插和尾插，不需要反转
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> list = new LinkedList<>();
        if (root == null) return list;
        queue.offer(root);
        boolean isOdd = true;//偶数
        while (!queue.isEmpty()) {
            Deque<Integer> clist = new LinkedList<>();
            int len = queue.size();
            for (int i = 0; i < len; i++) {
                TreeNode node = queue.poll();
                if (isOdd) {
                    clist.addLast(node.val);
                } else {
                    clist.addFirst(node.val);
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            isOdd = !isOdd;
            list.add(new LinkedList<>(clist));
        }
        return list;
    }

    public static void main(String[] args) {
        Deque<Integer> deque = new LinkedList<>();
        deque.addLast(1);
        deque.addLast(2);
        deque.addLast(3);
        System.out.println(deque);
    }
}
